'''
Test routines for the g.py module.
'''

from __future__ import print_function, division
import sys
import os
from g import *

def testClipping():
    '''This routine will construct a trapezoid and put a picture 
    inside it.  Then it will draw vertical black lines and a red border
    around the trapezoid.  There should be no picture or marks outside
    the trapezoid, since the clipping region is set to the trapezoid.
    '''
    s = open("out/blue_boat_covers.ps", "w")
    ginitialize(s)
    setOrientation(portrait, inches)
    translate(1, 1)
    base   = 6
    height = 8
    newPath()
    pathAddPoint(0, 0)
    pathAddPoint(base, 0)
    pathAddPoint(2*base/3, height)
    pathAddPoint(base/3, height)
    pathClose()
    p = getPath()
    comment("Now set the clipping limits")
    clip(p)
    # Put a picture inside the triangle
    move(-1, -1)
    offset = 4
    try:
        picture("pictures/blue_boat_covers.png", base+offset, height+offset)
    except AttributeError:
        print("Call to picture() in testClipping() failed")
    # Now draw some lines.  They should only be seen inside the triangle.
    lineWidth(.1)
    lineColor(black)
    for ix in xrange(base*2):
        x = ix/2.
        line(x, 0, x, height)
    # Put a red line around the triangle
    lineWidth(.05)
    lineColor(red)
    try:
        drawPath()
    except Exception:
        # Ignore for now until can fix this
        pass

def testLineFill():
    s = open("out/test_line_fill.ps", "w")
    ginitialize(s)
    setOrientation(portrait, inches)
    # Draw a black rectangle filled with green lines at 45 degrees
    comment("Black rectangle filled with green lines", yes)
    lineWidth(.02)
    x, y = .5, .5
    w, h =  3,  2
    lineColor(black)
    fillType(line_fill)
    fillColor(green)
    lineFill(45)
    fillOn()
    move(x, y)
    rectangle(w, h)
    # Draw a rectangle with black fill, then fill it with blue dashed lines
    comment("Black filled rectangle with blue dashed lines", yes)
    lineWidth(.05)
    x, y = .5, 3
    fillType(solid_fill)
    fillColor(black)
    lineOff()
    move(x, y)
    rectangle(w, h)
    fillType(line_fill)
    lineColor(gold)
    fillColor(blue)
    lineFillType(dash_dot)
    lineFillWidth(.02)
    lineFill(135, .2)
    lineOn()
    move(x, y)
    rectangle(w, h)
    # Draw a rectangle cross-hatched with green lines and no border
    comment("Rectangle cross-hatched with green lines and no border", yes)
    lineWidth(.02)
    x, y = .5, 5.5
    spacing = .1
    lineColor(black)
    fillType(line_fill)
    fillColor(green)
    fillOn()
    lineFillWidth(.01)
    lineFill(45, spacing)
    lineFillType(solid_line)
    lineOff()
    move(x, y)
    rectangle(w, h)
    lineFill(135, spacing)
    rectangle(w, h)
    # Rectangle with red lines at 0 degrees
    comment("Rectangle filled with red lines at 0 degrees", yes)
    lineWidth(.02)
    x, y = .5, 8
    lineOff()
    fillType(line_fill)
    fillColor(red)
    lineFill(0)
    fillOn()
    move(x, y)
    rectangle(w, h)
    # Rectangle with red lines at 90 degrees
    comment("Rectangle filled with red lines at 90 degrees", yes)
    lineWidth(.02)
    x, y = 4, .5
    lineOff()
    fillType(line_fill)
    fillColor(red)
    lineFill(90)
    fillOn()
    move(x, y)
    rectangle(w, h)
    # Rectangle using the phase offset to alternate line types
    comment("Rectangle filled with alternating line types", yes)
    lineWidth(.02)
    x, y = 4, 3
    lineOn()
    fillType(line_fill)
    fillColor(red)
    separation = .2
    phase      = separation/2.
    lineFill(45, separation)
    fillOn()
    move(x, y)
    rectangle(w, h)
    lineFillType(dashed)
    lineFill(45, separation, phase)
    rectangle(w, h)
    # Rectangle with double cross-hatching
    comment("Rectangle double cross-hatched", yes)
    lineWidth(.01)
    x, y = 4, 5.5
    spacing = .3
    phase = spacing/2.
    lineColor(black)
    lineOff()
    fillType(line_fill)
    fillColor(navy)
    fillOn()
    lineFillWidth(.04)
    lineFillType(solid_line)
    move(x, y)
    lineFill(45, spacing, phase)
    rectangle(w, h)
    lineFill(135, spacing, phase)
    rectangle(w, h)
    lineFill(0, spacing, phase)
    rectangle(w, h)
    lineFill(90, spacing, phase)
    rectangle(w, h)
    # "Inverse video" filling by wide lines
    comment("\"Inverse video\" filling by wide lines", yes)
    lw = .5
    lineWidth(1)
    x, y = 4, 8
    spacing = lw + .02
    lineColor(black)
    lineOff()
    fillType(line_fill)
    fillColor(darkgreen)
    fillOn()
    lineFillWidth(lw)
    lineFillType(solid_line)
    move(x, y)
    lineFill(45, spacing)
    rectangle(w, h)

def testEllipticalArcLineFill():
    '''Draw arcs, circles, & ellipses with various line fills.
    '''
    s = open("out/ellip_arc_line_fill.ps", "w")
    ginitialize(s)
    setOrientation(portrait, inches)
    comment("Undertall elliptical arc", yes)
    fillOn()
    lineOn()
    fillColor(red)
    fillType(line_fill)
    lineFill(45)
    maj = 3
    min = 1
    move(2, .5)
    ellipticalArc(maj, min, 0, 135)
    comment("Undertall ellipse", yes)
    move (2, 2)
    ellipticalArc(maj, min, 0, 360)
    comment("Circle with dashed lines", yes)
    fillColor(blue)
    lineFill(-45)
    lineFillType(dash_dot_dot)
    move (2, 5)
    ellipticalArc(maj, maj, 0, 360)
    comment("Green-filled ellipse with bold red border", yes)
    fillColor(green)
    lineColor(red)
    lineWidth(.05)
    lineFillType(solid_line)
    line_width = .3
    lineFillWidth(line_width)
    lineFill(45, 1.05*line_width)
    move (2, 8.5)
    ellipticalArc(maj, .8*maj, 0, 360)

def testPolygon():
    '''Draw a polygon.
    '''
    ofp = open("out/polygon.ps", "w")
    ginitialize(ofp, wrap_in_PJL=no)
    setOrientation(portrait, inches)
    fillColor(blue)
    fillOn()
    translate(4, 5)  # Put origin at center of page
    diameter = 7
    n = 6
    angle_offset = 360./(2*n)
    # Draw some outside lines for reference
    r = 1.2*diameter/2
    line(-r, 0, r, 0)
    line(0, -r, 0, r)
    move(0, 0)
    regularPolygon(diameter, n, angle_offset)
    lineColor(white)
    r = diameter/2.
    line(-r, 0, r, 0)
    line(0, -r, 0, r)
    gclose()

if __name__ == "__main__":
    # Make sure the out directory exists
    if not os.path.isdir("out"):
        os.mkdir("out")
    testClipping()
    testLineFill()
    testEllipticalArcLineFill()
    testPolygon()
